package com.office.anywher.poly.list.email;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.office.anywher.R;
import com.office.anywher.execeptions.OAException;
import com.office.anywher.https.HttpClientService;
import com.office.anywher.poly.list.Common;
import com.office.anywher.poly.list.IDataReceiveForList;
import com.wenxy.common.IConst;

public class ListDraffBoxEmail extends Common implements IDataReceiveForList{

	@Override
	public List<HashMap<String, Object>> receive(HttpClientService aService,int start,int rowNum) throws OAException {
		List<HashMap<String, Object>> rtnList = new ArrayList<HashMap<String, Object>>();
		JSONArray aJsonArray = aService.getEmailList(IConst.Http.Email.DraffBox.URL,null, start, rowNum);
		if(aJsonArray==null || aJsonArray.length()<=0){throw new OAException(OAException.OA_EXECEPTION_DEF[3]);}
		for(int i = 0;i<aJsonArray.length();i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			JSONObject aJsoObj = null;
			try {
				aJsoObj = aJsonArray.getJSONObject(i);
				if(aJsoObj == null)continue;
				map.put(IConst.Http.Email.DraffBox.OUT.ITEM_IMAGE, R.drawable.email_unread);
				map.put(IConst.Http.Email.DraffBox.OUT.STAT,isNullInJsonObject(aJsoObj,IConst.Http.Email.DraffBox.OUT.STAT)?"  ":aJsoObj.getString(IConst.Http.Email.DraffBox.OUT.STAT));
				map.put(IConst.Http.Email.DraffBox.OUT.SENDER,isNullInJsonObject(aJsoObj,IConst.Http.Email.DraffBox.OUT.SENDER)?"  ":aJsoObj.getString(IConst.Http.Email.DraffBox.OUT.SENDER));
				map.put(IConst.Http.Email.DraffBox.OUT.CREATE_TIME,isNullInJsonObject(aJsoObj,IConst.Http.Email.DraffBox.OUT.CREATE_TIME)?"  ":aJsoObj.getString(IConst.Http.Email.DraffBox.OUT.CREATE_TIME));
				map.put(IConst.Http.Email.DraffBox.OUT.EMAIL_TITLE,isNullInJsonObject(aJsoObj,IConst.Http.Email.DraffBox.OUT.EMAIL_TITLE)?"  ":aJsoObj.getString(IConst.Http.Email.DraffBox.OUT.EMAIL_TITLE));
				map.put(IConst.Http.Email.DraffBox.OUT.ID,isNullInJsonObject(aJsoObj,IConst.Http.Email.DraffBox.OUT.ID)?"  ":aJsoObj.getString(IConst.Http.Email.DraffBox.OUT.ID));
				if(!isNullInJsonObject(aJsoObj,IConst.Http.Email.DraffBox.OUT.ATTACH)){
					if(aJsoObj.getBoolean(IConst.Http.Email.DraffBox.OUT.ATTACH)){
						map.put(IConst.Http.Email.DraffBox.OUT.ATTACH, R.drawable.attach);
					}else{
						map.put(IConst.Http.Email.DraffBox.OUT.ATTACH, R.drawable.use_empty);
					}
				}else{
					map.put(IConst.Http.Email.DraffBox.OUT.ATTACH, R.drawable.use_empty);
				}
				rtnList.add(map);
			} catch (JSONException e) {
				e.printStackTrace();
				throw new OAException(OAException.OA_EXECEPTION_DEF[2]);
			}
		}
		return rtnList;
	}
}
